package com.faithbj.device.web.controller.admin;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.faithbj.device.model.entity.Consumable;
import com.faithbj.device.model.entity.Stock;
import com.faithbj.device.model.entity.StockItem;
import com.faithbj.device.service.ConsumableService;
import com.faithbj.device.service.StockItemService;
import com.faithbj.device.service.StockService;

@Controller
@RequestMapping("/faith/stockitem/")
public class AdminStockItemController extends BaseAdminController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4784506615400387088L;
	
	private final String OPERRATE_RETURN_URL="list/";
	private final String INLIST="admin/instock_item_info_list";
	
	private final String OUTLIST="admin/outstock_item_info_list";

	@Resource
	private StockService stockService;
	@Resource
	private StockItemService stockItemService;
	@Resource
	private ConsumableService consumableService;
	
	// 列表
		@RequestMapping("list/{stockid}/{isInStock}")
		public String list(ModelMap map,@PathVariable String stockid,@PathVariable int isInStock) {
			Stock stock = stockService.load(stockid);
			pager = stockItemService.findByPager(pager,stock);
			map.put("pager", pager);
			if(isInStock==1){
				return INLIST;
			}
			return OUTLIST;
		}
		
		
		// 删除
		@RequestMapping("delete/{id}/{isInStock}")
		public String delete(@PathVariable String id,ModelMap map,@PathVariable String isInStock){
			StockItem  st = stockItemService.load(id);
			Consumable con = st.getConsumable();
			Stock stock = st.getStock();
			if(isInStock.equals("1")){
				stock.setPrice(stock.getPrice().subtract(st.getPrice()));
				con.setCount(con.getCount()-st.getCount());
				con.setInCount(con.getInCount()-st.getCount());
			}else if(isInStock.equals("0")){
				con.setCount(con.getCount()+st.getCount());
				con.setOutCount(con.getOutCount()-st.getCount());
				stock.setPrice(stock.getPrice().subtract(st.getPrice()));
			}else{
				map.put("errorMessages", "出入库类型不明确");
				return ERROR;
			}
			consumableService.update(con);
			stockService.update(stock);
			stockItemService.delete(id);
			
			map.put("redirectUrl", "../../"+OPERRATE_RETURN_URL+stock.getId()+"/"+isInStock);
			return SUCCESS;
		}


}
